﻿using System;
using System.Net;
using System.Linq;
using System.Collections.Generic;

namespace Tulde.Windows.Music.Mp3Tunes
{
    public enum OutputTypes
    {
        json,
        xml
    }
    public enum ServiceTypes
    {
        artist,
        album,
        track,
        playlist
    }
    public enum ServiceKeys
    {
        artist_id,
        album_id,
        track_id,
        playlist_id
    }
    public enum ResultDetail
    {
        max,
        min
    }
    public class ServiceParameters 
    {
        protected T GetAsType<T>(string parameter, Func<string, T> parser = null)
        {
            if (parser == null)
            {
                try
                {
                    return (T)Convert.ChangeType(parameter, typeof(T));
                }
                catch
                {
                    return default(T);
                }
            }
            else
            {
                return parser(parameter);
            }
        }
        private Dictionary<string, string> parameters = new Dictionary<string, string>();
        protected void SetParameter( string key, string value)
        {
            if (this.parameters.ContainsKey(key))
            {
                this.parameters[key] = value;
            }
            else
            {
                this.parameters.Add(key, value);
            }
        }
        public IEnumerable<string> Keys
        {
            get
            {
                return this.parameters.Keys.AsEnumerable();
            }
        }
        public string Remove( string key)
        {
            if (this.parameters.ContainsKey(key))
            {
                string r = this.parameters[key];
                this.parameters.Remove(key);
                return r;
            }
            else
            {
                return null;
            }
        }
        public string GetParameter(string key)
        {
            if (this.parameters.ContainsKey(key))
            {
                return this.parameters[key];
            }
            else
            {
                return null;
            }
        }

        public string SessionId
        {
            get
            {
                return this.GetParameter(Constants.SessionId);
            }
            set
            {
                this.SetParameter(Constants.SessionId, value);
            }
        }
        public string PartnerToken
        {
            get
            {
                return this.GetParameter(Constants.PartnerToken);
            }
            set
            {
                this.SetParameter(Constants.PartnerToken, value);
            }
        }
        public OutputTypes OutputType
        {
            get
            {
                return (OutputTypes)Enum.Parse(typeof(OutputTypes), this.GetParameter(Constants.Output));
            }
            set
            {
                this.SetParameter(Constants.Output, value.ToString());
            }
        }

        public ServiceParameters()
        {
            this.OutputType = OutputTypes.json;
            this.PartnerToken = SecurityProvider.Credentials.Domain;
        }
    }
    public class DataServiceParameters : ServiceParameters
    {
        
        public ServiceTypes ServiceType
        {
            get
            {
                return (ServiceTypes)Enum.Parse(typeof(ServiceTypes), this.GetParameter(Constants.Type));
            }
            set
            {
                this.SetParameter(Constants.Type, value.ToString());
            }
        }
        public string PlaylistServiceKey
        {
            get
            {
                return this.GetParameter(ServiceKeys.playlist_id.ToString());
            }
            set
            {
                this.SetParameter(ServiceKeys.playlist_id.ToString(), value);
            }
        }
        public string ArtistServiceKey
        {
            get
            {
                return this.GetParameter(ServiceKeys.artist_id.ToString());
            }
            set
            {
                this.SetParameter(ServiceKeys.artist_id.ToString(), value);
            }
        }
        public string AlbumServiceKey
        {
            get
            {
                return this.GetParameter(ServiceKeys.album_id.ToString());
            }
            set
            {
                this.SetParameter(ServiceKeys.album_id.ToString(), value);
            }
        }

        public bool MiniData
        {
            get
            {
                return this.GetAsType<bool>(this.GetParameter(Constants.MiniData));
            }
            set
            {
                this.SetParameter(Constants.MiniData, value.ToString().ToLower());
            }
        }    
        

        public DataServiceParameters()
        {
            this.SessionId = SecurityProvider.Token.SessionID;
            this.MiniData = false;
           // this.SetParameter("result_data_level","max");
            
        }
    }
    public class DataServiceSearchParameters : DataServiceParameters
    {
        public string SearchPattern
        {
            get
            {
                return this.GetParameter(Constants.SearchPattern);
            }
            set
            {
                this.SetParameter(Constants.SearchPattern, value);
            }
        }
        public ResultDetail ResultDataLevel
        {
            get
            {
                return (ResultDetail)Enum.Parse(typeof(ResultDetail), this.GetParameter(Constants.ResultDataLevel));
            }
            set
            {
                this.SetParameter(Constants.ResultDataLevel, value.ToString());
            }
        }
       
        public DataServiceSearchParameters()
        {
            this.ResultDataLevel = ResultDetail.min;
            
        
        }
    }
    public class AuthenticationServiceParameters : ServiceParameters
    {
        public AuthenticationServiceParameters()
        {

        }
        public string UserName
        {
            get
            {
                return this.GetParameter(Constants.UserName);
            }
            set
            {
                this.SetParameter(Constants.UserName, value);
            }
        }
        public string Password
        {
            get
            {
                return this.GetParameter(Constants.Password);
            }
            set
            {
                this.SetParameter(Constants.Password, value);
            }
        }
    }
}
